package com.MoveEat.activities;

import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;

import com.MoveEat.Utils.Consts;
import com.MoveEat.Utils.Utils;
import com.MoveEat.Utils.Consts.DB_COLUMNS;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.os.AsyncTask;
import android.widget.Toast;

public class ActivityWrapper extends Activity
{
	private AsyncTask<?, ?, ?>			mLoadingTask;
	private ProgressDialog				mLoadingDialog;
	protected HashMap<Integer, String>	mCatHashMap;

	public void showLoading(final AsyncTask<?, ?, ?> task)
	{
		mLoadingTask = task;
		showDialog(ProgressDialog.STYLE_HORIZONTAL);
	}

	@Override
	protected Dialog onCreateDialog(int id)
	{
		mLoadingDialog = new ProgressDialog(this);
		mLoadingDialog.setIndeterminate(true);
		mLoadingDialog.setMessage("Loading");
		mLoadingDialog.setCancelable(true);
		mLoadingDialog.setOnCancelListener(new OnCancelListener()
		{

			@Override
			public void onCancel(DialogInterface arg0)
			{
				Toast.makeText(getApplicationContext(), "Canceled", Toast.LENGTH_LONG);
				if (mLoadingTask != null)
				{
					mLoadingTask.cancel(true);
				}
			}

		});
		return mLoadingDialog;
	}

	public ProgressDialog getLoadingDialog()
	{
		return mLoadingDialog;
	}

	protected void getAllCategories()
	{
		String catList = Utils.preformRequest(Utils.BASE_URL + "GetMenus.php?getAllCat");
		if (catList == null)
		{
			return;
		}
		mCatHashMap = new HashMap<Integer, String>();
		JSONArray arr = null;
		try
		{
			arr = new JSONArray(catList);
		}
		catch (JSONException e)
		{
			Utils.log(-1,"Error Creating JSONArray");
		}
		if (arr != null)
		{
			for (int i = 0; i < arr.length(); i++)
			{
				mCatHashMap.put(arr.optJSONObject(i).optInt(Consts.DB_COLUMNS.CAT_ID), arr.optJSONObject(i).optString(Consts.DB_COLUMNS.CAT_NAME));
			}
		}
	}

}
